<?php

namespace app\admin\controller;


use app\common\model\Classes;
use app\common\model\ClassesXueyuan;
use app\common\model\Config;
use app\common\model\Kaoqin as KaoqinModel;
use app\common\model\KaoqinXueyuan;
use app\common\model\Kaoqin;
use app\common\model\ManagerGongziZhaoxin;
use app\common\model\XueyuanJiaofei;
use app\common\model\Xueyuan as XueyuanModel;
use app\common\model\Manager;
use think\Db;

class Xueyuan extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分页获取信息
     * @return void
     */
    public function list()
    {
        //查询条件
        $where = array(
            'xueyuan.status' => array('eq', 1)
        );

        //班级内学员
        if (input('mid')) {
            $cids = Classes::getColumn(array('mid' => input('mid')), 'id');
            $sids = ClassesXueyuan::getColumn(array('cid' => array('in', $cids)), 'sid');
            $where['xueyuan.id'] = array('in', $sids);
        }

        //排序
        $order = 'xueyuan.id desc';

        //条件
        if (input('id')) {
            if (input('mid')) {
                if (in_array(input('id'), $sids)) {
                    $where['xueyuan.id'] = input('id');
                } else {
                    $where['xueyuan.id'] = 0;
                }
            }else{
                $where['xueyuan.id'] = input('id');
            }
        }

        if (input('name')) {
            $where['xueyuan.name'] = array('like', "%" . input('name') . "%");
        }
        if (input('taocan')) {
            $where['b.taocan'] = array('eq', (int)input('taocan'));
        }
        if (input('order')) {
            $order = 'lose_time asc';
        }

        //分页信息
        $page = max(input('page'), 1);
        $size = input('limit', 20);
        //获取分页列表
        $list = Db::table('xueyuan')->alias('a')
            ->join('xueyuan_jiaofei b', 'a.now_jid=b.id', 'left')
            ->limit(($page - 1) * $size, $size)
            ->field('a.*,b.pay_money,b.pay_time,b.month,b.lose_time,b.send_month,b.class_hour,b.has_class_hour,b.taocan,b.tuijian')
            ->where($where)
            ->order($order)
            ->select()
            ->toArray();

        //总页码
        $total = Db::table('xueyuan')->alias('a')
            ->join('xueyuan_jiaofei b', 'a.now_jid=b.id', 'left')
            ->limit(($page - 1) * $size, $size)
            ->field('a.*,b.pay_money,b.pay_time,b.month,b.lose_time,b.send_month,b.class_hour,b.has_class_hour,b.taocan')
            ->where($where)
            ->order($order)
            ->count();
        //数据返回
        if (!$list) {
            return returnJson(0, '', array('list' => [], 'total' => 0));
        } else {
            foreach ($list as $k => $v) {
                $jw = array(
                    'sid' => $v['id']
                );
                $list[$k]['cids'] = ClassesXueyuan::getColumn($jw, 'cid');
            }
        }
        return returnJson(0, '', [
            'list' => $list,
            'size' => $size,
            'total' => $total
        ]);
    }

    /**
     * 学员信息
     * @return \think\response\Json
     */
    public function detail()
    {
        //查询条件
        $where = array('a.id' => array('eq', input('id')));

        //获取信息
        $detail = Db::table('xueyuan')->alias('a')
            ->join('xueyuan_jiaofei b', 'a.now_jid=b.id')
            ->field('a.*,b.pay_money,b.pay_time,b.month,b.lose_time,b.send_month,b.class_hour,b.has_class_hour,b.taocan')
            ->where($where)
            ->find();
        $detail['images'] = json_decode($detail['images'], true);

        $detail['cids'] = ClassesXueyuan::getColumn(['sid' => $detail['id']], 'cid');

        //配置信息
        $c_w = array(
            'name' => array('in', ['keshi_shoufei', 'keshi', 'web_domain'])
        );
        $config = Config::getAll($c_w);
        $c = array();
        foreach ($config as $k => $v) {
            $c[$v['name']] = $v['value'];
        }

        //返回信息
        return returnJson(0, '', array(
            'detail' => $detail,
            'config' => $c
        ));
    }

    /**
     * 学员删除
     * @return void
     */
    public function delete()
    {
        // 查询条件
        $where = [];

        if (input('id')) {
            $where['id'] = array('eq', input('id'));
        }

        XueyuanModel::update(array('status' => 0), $where);

        //返回信息
        return returnJson(0, '');
    }


    /**
     * 缴费记录
     * @return void
     */
    public function jiaofei()
    {
        // 查询条件
        $where = [];

        if (input('id')) {
            $where['sid'] = array('eq', input('id'));
        }

        //获取列表
        $list = XueyuanJiaofei::getAll($where, 'lose_time desc');

        //考勤id
        $kids = KaoqinXueyuan::getColumn($where, 'kid', 'id desc');


        if($list){
            //考勤查询条件
            $k_w = array(
                'id' => array('in', $kids)
            );
            foreach ($list as &$v){
                $v['time'] = ManagerGongziZhaoxin::getValue(array('jfid'=>$v['id']),'time');
                $v['manager_name'] = Manager::getValue(array('id'=>$v['mid']),'email');
                $k_w['time'] = array(
                    '>=' => date('Y-m-d', $v['pay_time']),
                    '<=', date('Y-m-d', $v['lose_time'])
                );

                $data = Kaoqin::getAll($k_w,'id desc');
                $v['chuqin'] = Kaoqin::getCount($k_w);

                if (!$d) {
                    $d = [];
                }
                $v['list'] = $data;

            }
        }

        //返回信息
        return returnJson(0, '', $list);
    }

    /**
     * 新增或修改学员基本信息
     * @return \think\response\Json|void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $input = input();
            $time = time();
            $data = array(
                'name' => $input['name'],
                'sex' => $input['sex'],
                'birth' => $input['birth'],
                'qiufu_name' => $input['qiufu_name'],
                'qiufu_number' => $input['qiufu_number'],
                'warranty' => $input['warranty'],
                'phone' => $input['phone'],
                'jinji' => $input['jinji'],
                'images' => json_encode($input['images']),
                'idcard' => $input['idcard'],
                'tuijian' => $input['tuijian'],
                'join_time' => $input['join_time'],
                'address' => $input['address'],
                'height' => $input['height'],
                'weight' => $input['weight'],
                'update_time' => $time,
            );
            if ($input['id']) {
                $data['id'] = $input['id'];
            } else {
                $data['create_time'] = $time;
            }

            if ($data['id']) {
                $res = XueyuanModel::update($data, array('id' => $data['id']));
                $res = $data['id'];
            } else {
                $res = XueyuanModel::setInsertGetId($data);
            }

            if ($input['cids']) {
                $classes_xueyuan = [];

                foreach ($input['cids'] as $v) {
                    $c = array(
                        'cid' => $v,
                        'sid' => $res,
                        'update_time' => time(),
                        'create_time' => time()
                    );
                    $classes_xueyuan[] = $c;
                }
                ClassesXueyuan::setDel(array('sid' => $res));
                ClassesXueyuan::setSaveAll($classes_xueyuan);
            }

            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }

    /**
     * 通过id获取缴费信息
     * @return \think\response\Json
     */
    public function jiaofeiDetail()
    {
        // 查询条件
        $where = [];

        if (input('id')) {
            $where['id'] = array('eq', input('id'));
        }

        //获取信息
        $data = XueyuanJiaofei::getOne($where);
        $data['pay_time'] = date('Y-m-d H:i:s', $data['pay_time']);


        $time = ManagerGongziZhaoxin::getValue(array('jfid'=>$data['id']),'time');
        $data['time'] = $time;

        //返回信息
        return returnJson(0, '', $data);
    }


    /**
     * 更新或修改学员缴费信息
     * @return \think\response\Json|void
     */
    public function updateJiaofei()
    {
        if (request()->isPost()) {
            $input = input();
            $time = time();
            $date = new \DateTime($input['pay_time']);
            $month = input('month') + input('send_month');
            $date->modify('+' . $month . ' month');
            $data = array(
                'sid' => $input['sid'],
                'pay_money' => $input['pay_money'],
                'pay_time' => strtotime($input['pay_time']),
                'month' => $input['month'],
                'lose_time' => $date->getTimestamp(),
                'send_month' => $input['send_month'],
                'tuijian' => $input['tuijian'],
                'class_hour' => $input['class_hour'],
                'has_class_hour' => $input['has_class_hour'],
                'update_time' => $time,
                'taocan' => $input['taocan'],
                'is_now' => $input['is_now'],
                'mid' => $input['mid']

            );
            if ($input['id']) {
                $data['id'] = $input['id'];
            } else {
                $data['create_time'] = $time;
            }

            if ($data['id']) {
                $res = XueyuanJiaofei::update($data, array('id' => $data['id']));
                $res = $data['id'];
            } else {
                $res = XueyuanJiaofei::setInsertGetId($data);
                //招新
                $zhaoxin_save = array(
                    'sid' => $input['sid'],
                    'time' => $input['time'],
                    'mid' => $input['mid'],
                    'jfid' => $res,
                    'update_time' => $time,
                    'create_time' => $time
                );
                ManagerGongziZhaoxin::setInsertGetId($zhaoxin_save);
            }

            if ($input['is_now'] == 1) {
                $save = array(
                    'is_now' => 0
                );
                XueyuanJiaofei::update($save, array('sid' => $input['sid'], 'is_now' => array('<=', 1)));
                $save = array(
                    'is_now' => 1
                );
                XueyuanJiaofei::update($save, array('id' => $data['id']));
                $save = array(
                    'now_jid' => $res
                );
                XueyuanModel::update($save, array('id' => $input['sid']));
            }

            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }


    public function xueyuanByCid()
    {
        if (input('cid')) {
            $where = array(
                'cid' => input('cid')
            );
            $sids = ClassesXueyuan::getColumn($where, 'sid');
            $x_w['id'] = array('in', $sids);
        }

        $x_w['status'] = 1;

        $list = XueyuanModel::getAll($x_w, '', 'id,name');

        return returnJson(0, '', $list);
    }

    /**
     * 学员反馈获取学员信息
     * @return void
     */
    public function fankuiXueyuan()
    {
        //查询条件
        $where = array('id'=>input('id'));

        //学员信息
        $xueyuan = XueyuanModel::getOne($where);

        //学员年龄
        $xueyuan['age'] = date('Y')-substr($xueyuan['idcard'],6,4);

        //学员球龄
        $xueyuan['qiuling'] = date('Y')-date('Y',strtotime($xueyuan['join_time']));

        //出勤次数
        $kids = KaoqinXueyuan::getColumn(array('sid'=>input('id')),'kid');
        if ($kids){
            $date = date('Y-m',strtotime(input('date').'-1'));
            $w = array(
                'id'=>array('in',$kids),
                'time'=>array('like',$date.'%')
            );
            $xueyuan['cishu'] = Kaoqin::getCount($w);
        }
        if(!$xueyuan['cishu']){
            $xueyuan['cishu'] = 0;
        }
        //评估日期
        $xueyuan['riqi'] = date('Y-m-d');

        return returnJson(0, '', $xueyuan);

    }

    /**
     * 修改快递单号
     * @return \think\response\Json|void
     */
    public function updateDanhao()
    {
        if (request()->isPost()) {
            //更新数据
            $time = time();
            $data = array(
                'warranty' => input('warranty'),
                'update_time' => $time,
            );

            //更新
            $res = false;
            if (input('sid')) {
                $res = XueyuanModel::update($data, array('id' => input('sid')));
            }

            //返回信息
            if ($res) {
                return returnJson(0, '操作成功');
            }
            return returnJson(1, '网络异常，请重试');
        }
    }


    /**
     * 缴费信息删除
     * @return \think\response\Json
     */
    public function deletejiaofei()
    {
        if (input('id', 0)) {
            //查询条件
            $where['id'] = input('id');
            $jiaofei = XueyuanJiaofei::getOne($where);

            //修改当前选中的缴费记录
            $save['now_jid'] = 0;
            XueyuanModel::update($save,array('now_jid'=>input('id')));

            //删除记录
            XueyuanJiaofei::setDel($where);
        }
        return returnJson(0, '');

    }
}
